#!/usr/bin/python
# -*- coding:UTF8 -*-
from lxml import etree  # xpath的定位方法
import requests  # 请求方式
import os  # 建文件夹
import re  # 正则
path = '测试数据/人教版高中语文必修一PPT课件'
if not os.path.exists(path):
    os.makedirs(path)
url = 'https://www.1ppt.com/kejian/yuwen/780/'
user_agent = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'}
r = requests.get(url=url, headers=user_agent)  # 1.获取尾页的定位信息
r.encoding = 'gbk'
analytical_data = etree.HTML(r.text)  # 解析数据
tail_page = analytical_data.xpath('//a[text()="末页"]//@href')
if tail_page != []:  # 用正则做判断，返回多少页
    tail_page = re.search('(\d+).html', tail_page[0])
    tail_page = tail_page.group(1)
else:
    tail_page = 1
for tail_page in range(1, int(tail_page) + 1):  # 翻页方式获取每个页面下的详细文件
    if tail_page == 1:
        url = 'https://www.1ppt.com/kejian/yuwen/780/'
    else:
        url = 'https://www.1ppt.com/kejian/yuwen/780/' + 'kejian_' + str(tail_page) + '.html'
    r = requests.get(url=url, headers=user_agent)  # 获取所有页面的信息
    r.encoding = 'gbk'
    analytical_data = etree.HTML(r.text)
    coursewore_name = analytical_data.xpath('//ul[@class="arclist"]//h2//a/text()')  # 课件名称定位
    coursewore_href = analytical_data.xpath('//ul[@class="arclist"]//h2//@href')  # 课件链接定位
    coursewore_href = ['https://www.1ppt.com' + coursewore_href for coursewore_href in coursewore_href]
    dict1 = dict(zip(coursewore_name, coursewore_href))
    for coursewore_name, coursewore_href in dict1.items():
        r = requests.get(url=coursewore_href, headers=user_agent)  # 获取单个课件下载链接
        r.encoding = 'gbk'
        analytical_data = etree.HTML(r.text)
        down_href = analytical_data.xpath('//ul[@class="downurllist"]//@href')
        down_href = ['https://www.1ppt.com' + down_href for down_href in down_href]
        r = requests.get(url=down_href[0], headers=user_agent)  # 获取下载链接（电信通道等等）
        r.encoding = 'gbk'
        analytical_data = etree.HTML(r.text)
        down_url = analytical_data.xpath('//li[@class="c1"]//@href')
        r = requests.get(url=down_url[0], headers=user_agent).content  # 二进制写入
        suffix = down_url[0].split('.')[-1]
        file_path = path + '/' + coursewore_name + '.' + suffix  # 拼接出文件路径
        with open(file_path, 'wb') as file:
            file.write(r)
            print(file_path, '下载成功！！！')
